<template>
  <el-dialog
    title="更新用户信息"
    :visible.sync="visible"
    width="500px"
    :close-on-click-modal="false"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="100px"
    >
      <!-- 移除用户ID的显示和编辑，但在数据中保留 -->
      <el-form-item label="用户账号" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="居住地址" prop="address">
        <el-input v-model="form.address"></el-input>
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>

      <el-form-item label="班级名称" prop="className">
        <el-input v-model="form.className"></el-input>
      </el-form-item>

      <el-form-item label="简介" prop="introduction">
        <el-input type="textarea" v-model="form.introduction"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'EditDialog',
  data() {
    return {
      visible: false,
      form: {
        id: '', // 保留ID字段但不显示
        username: '',
        name: '',
        address: '',
        phone: '',
        className: '',
        introduction: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户账号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
        address: [{ required: true, message: '请输入居住地址', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        className: [{ required: true, message: '请输入班级名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open(data) {
      this.visible = true
      // 创建一个新对象来避免直接引用
      this.form = {
        ...data,
        id: data.id // 确保ID被保留但不可编辑
      }
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 提交时保留原始ID
          const submitData = {
            ...this.form,
            id: this.form.id // 确保使用原始ID
          }
          this.$emit('submit', submitData)
          this.visible = false
        }
      })
    }
  }
}
</script> 